﻿using BlockeyEssentials;
using BlockeyEssentials.Drawing;
using BlockeyEssentials.Input;
using BlockeyEssentials.ScreenSystem;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Blockey.GameplayScreens
{
    public class ControllerActionScreen : Screen
    {
        public ControllerActionScreen(ScreenManager oManager)
            : base(oManager)
        {
            _DirectionPosition = new Vector2(All.Resolution.Left + 100, All.Resolution.Bottom - 100);
            _DirectionShift = 25;
            _ButtonAColor = Color.Green;
            _ButtonBColor = Color.Red;

            var oTexture = All.Texture_Circle;
            var oOrigin = new Vector2(oTexture.Width, oTexture.Height) * 0.5f;

            _DirectionBaseSprite = new SpriteV(oTexture, _DirectionPosition, Vector2.One * 0.5f);
            _DirectionBaseSprite.Color = Color.DarkGray;
            _DirectionBaseSprite.Layer = 40;
            _DirectionBaseSprite.Origin = oOrigin;

            _DirectionSprite = new SpriteV(oTexture, _DirectionPosition, Vector2.One * 0.25f);
            _DirectionSprite.Color = Color.Gray;
            _DirectionSprite.Layer = 50;
            _DirectionSprite.Origin = oOrigin;

            var oButtonPosition = _DirectionPosition + Vector2.UnitX * 100;
            _ButtonASprite = new SpriteV(oTexture, oButtonPosition, Vector2.One * 0.5f);
            _ButtonASprite.Origin = oOrigin;
            _ButtonASprite.Layer = 50;
            _ButtonASprite.Color = _ButtonAColor;

            oButtonPosition = oButtonPosition + Vector2.UnitX * 100;
            _ButtonBSprite = new SpriteV(oTexture, oButtonPosition, Vector2.One * 0.5f);
            _ButtonBSprite.Origin = oOrigin;
            _ButtonBSprite.Layer = 50;
            _ButtonBSprite.Color = _ButtonBColor;

            _TextFont = new Font(All.Font_Debug);
            _TextFont.Color = Color.Black;
            _TextFont.Layer = 55;
        }

        #region Methods

        public override void Update()
        {
            var oDirection = Vector2.Zero;
            var oPressedA = false;
            var oPressedB = false;

            var oInput = InputManager.Keyboard;
            if (oInput.IsPressedOne(Keys.Up))
                oDirection.Y -= 1;
            if (oInput.IsPressedOne(Keys.Down))
                oDirection.Y += 1;
            if (oInput.IsPressedOne(Keys.Left))
                oDirection.X -= 1;
            if (oInput.IsPressedOne(Keys.Right))
                oDirection.X += 1;
            if (oInput.IsPressedOne(Keys.F))
                oPressedA = true;
            if (oInput.IsPressedOne(Keys.D))
                oPressedB = true;

            var oGamepadInput = InputManager.GamePad1;
            if (oDirection == Vector2.Zero)
            {
                oDirection = oGamepadInput.LeftStick;
                oDirection.Y *= -1;
            }
            if (oGamepadInput.IsPressed(Buttons.A))
                oPressedA = true;
            if (oGamepadInput.IsPressed(Buttons.B))
                oPressedB = true;

            if (oDirection != Vector2.Zero)
                oDirection.Normalize();

            _DirectionSprite.Position = _DirectionPosition + oDirection * _DirectionShift;

            if (oDirection.Length() > 0)
                _DirectionAngle = MathHelper.ToDegrees(All.VectorToAngle(oDirection)) + 180;
            else
                _DirectionAngle = -1;

            if (oPressedA)
                _ButtonASprite.Color = Color.Yellow;
            else
                _ButtonASprite.Color = _ButtonAColor;

            if (oPressedB)
                _ButtonBSprite.Color = Color.Yellow;
            else
                _ButtonBSprite.Color = _ButtonBColor;
        }

        public override void Draw(SpriteBatch oSB)
        {
            _DirectionBaseSprite.Draw(oSB, Transition);
            _DirectionSprite.Draw(oSB, Transition);

            _TextFont.Draw(oSB, Transition, _DirectionBaseSprite.Position, _DirectionAngle.ToString());

            _ButtonASprite.Draw(oSB, Transition);
            _ButtonBSprite.Draw(oSB, Transition);
        }

        #endregion

        private SpriteV _DirectionBaseSprite;
        private SpriteV _DirectionSprite;
        private Vector2 _DirectionPosition;
        private float _DirectionShift;
        private float _DirectionAngle;
        private Font _TextFont;

        private SpriteV _ButtonASprite;
        private Color _ButtonAColor;

        private SpriteV _ButtonBSprite;
        private Color _ButtonBColor;

    }
}
